package main

import "fmt"

//MARK 嵌入类型String方法覆盖
type ByteSize float64

const (
	//MARK 枚举
	_           = iota             // iota初始1 用一次*2
	KB ByteSize = 1 << (10 * iota) //这里iota 2
	MB                             //到再遇到一个赋值, 都是按上一个iota的表达式赋值 这里: 1 << (10 * 4)
	GB                             //1 << (10 * 8)
	TB
	PB
	EB
	ZB
	YB
)

////MARK 嵌入类型String方法覆盖
func (b ByteSize) String() string {
	switch {
	case b >= YB:
		return fmt.Sprintf("%.2fYB", b/YB) //MARK 这里必须是 %f 不能是 %v 不能会再次调String 无限递归
	case b >= ZB:
		return fmt.Sprintf("%.2fZB", b/ZB)
	case b >= EB:
		return fmt.Sprintf("%.2fEB", b/EB)
	case b >= PB:
		return fmt.Sprintf("%.2fPB", b/PB)
	case b >= TB:
		return fmt.Sprintf("%.2fTB", b/TB)
	case b >= GB:
		return fmt.Sprintf("%.2fGB", b/GB)
	case b >= MB:
		return fmt.Sprintf("%.2fMB", b/MB)
	case b >= KB:
		return fmt.Sprintf("%.2fKB", b/KB)
	}
	return fmt.Sprintf("%.2fB", b)
}
func main() {
	a := ByteSize(3450)
	fmt.Println(a)              //3.37KB
	fmt.Println(ByteSize(1e13)) // 9.09TB
	fmt.Println(KB)
	fmt.Println(MB)
	fmt.Println(GB)
}
